package com.iyjhsoc.game.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iyjhsoc.game.ENUM.ConstantENUM;
import com.iyjhsoc.game.ENUM.MethodsENUM;
import com.iyjhsoc.game.domain.VO.CompetitionSmallGameVO;
import com.iyjhsoc.game.domain.VO.CompetitionVO;
import com.iyjhsoc.game.domain.entity.CompetitionDO;
import com.iyjhsoc.game.domain.entity.TeamDO;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.service.CompetitionService;
import com.iyjhsoc.game.service.TeamService;
import com.iyjhsoc.game.util.RequestBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("iyj/competition")
@Slf4j
public class CompetitionController {
    @Resource
    CompetitionService competitionService;
    @Resource
    TeamService teamService;

    @PostMapping("getCompetitionList")
    public Response getCompetitionList(@RequestBody CompetitionVO vo){
        Response response = competitionService.queryList(vo);
        return response;
    }

    @GetMapping("syncKPLCompetition")
    public Response syncKPLCompetition(CompetitionVO vo){
        RequestBuilder requestBuilder = new RequestBuilder();
        String get = requestBuilder.url("https://pvp.qq.com/matchdata/scheduleDetails.html?league_id=20240001&match_id=2024041201").method(MethodsENUM.GET).execution();
        log.error(get);
        return Response.SUCCESS();
    }

    @PostMapping("saveCompetition")
    public Response saveCompetition(@RequestBody CompetitionDO competitionDO){
        TeamDO teamDO = teamService.getById(competitionDO.getTeamOneId());
        competitionDO.setTeamOneName(teamDO.getName());
        teamDO = teamService.getById(competitionDO.getTeamTowId());
        competitionDO.setTeamTowName(teamDO.getName());
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_one_id",competitionDO.getTeamOneId());
        queryWrapper.eq("team_tow_id",competitionDO.getTeamTowId());
        queryWrapper.eq("competition_date",competitionDO.getCompetitionDate());
        CompetitionDO one = competitionService.getOne(queryWrapper);
        if(one != null){
            return Response.ERROR(ConstantENUM.ALREADY_EXISTS);
        }
        boolean save = competitionService.save(competitionDO);
        if(save){
            return Response.SUCCESS(ConstantENUM.SUCCESS);
        }
        return Response.SUCCESS(ConstantENUM.ERROR);
    }
    @PostMapping("updateCompetition")
    public Response updateCompetition(@RequestBody CompetitionDO competitionDO){
        TeamDO teamDO = teamService.getById(competitionDO.getTeamOneId());
        competitionDO.setTeamOneName(teamDO.getName());
        teamDO = teamService.getById(competitionDO.getTeamTowId());
        competitionDO.setTeamTowName(teamDO.getName());
        boolean b = competitionService.updateById(competitionDO);
        if(b){
            return Response.SUCCESS(ConstantENUM.SUCCESS);
        }
        return Response.SUCCESS(ConstantENUM.ERROR);
    }

    @PostMapping("getById/{id}")
    public Response getById(@PathVariable("id") String id){
        Response response = competitionService.selectById(id);
        return response;
    }

    @PostMapping("getNotStartCompetition")
    public Response getNotStartCompetition(@RequestBody CompetitionVO vo){
        Response response = competitionService.getNotStartCompetition(vo);
        return response;
    }




}
